/**
 * TabletDao.java $version 1.0 Jul 3, 2012
 *
 * Copyright 2011 Pravin Kumar Corp. All rights Reserved.
 * Pravin Kumar PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.app.tablet.data.dao;

import java.sql.SQLException;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.app.tablet.data.db.TabletDB;
import com.app.tablet.view.AddPageActivity;

public class TabletDao extends BaseDao {

	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "TabletDao-Page ::";
	
	public static final String TABLE_NAME = "reminder";

	public static final String FIELD_TABLET_ID = "_id";
	public static final String FIELD_TABLET_NAME = "med_name";
	public static final String FIELD_DOSAGE = "dosage";
	public static final String FIELD_SCHEDULE = "schedule";
	public static final String FIELD_ALARM_TIME = "alarm_time";
	public static final String FIELD_TONES = "tones";

	private final String SQL_TABLET_LOAD_ALL = "select * from " + TABLE_NAME
			+ " ORDER BY "+ FIELD_TABLET_NAME;

	public static TabletDao getTabletDao() {
		return new TabletDao();
	}

	public Cursor getAllTablets(TabletDB handle) throws SQLException{
		Cursor result = null;
		SQLiteDatabase dbObj = null;
		try {
			dbObj = handle.getConnection(SQLiteDatabase.OPEN_READONLY);
			Log.i(TagProject,TagPage+"The query is "+SQL_TABLET_LOAD_ALL);
			result = dbObj.rawQuery(SQL_TABLET_LOAD_ALL,null);
			Log.i(TagProject,TagPage+"The query result is  "+result.getCount());
		} finally {
			
			if (dbObj != null) {
				dbObj.close();
			}
		}
		return result;
	}
	
	public synchronized void insertnew(TabletDB handle) throws SQLException {
		Log.i(TagProject, TagPage+"insertnew called...");
		SQLiteDatabase dbObj = null;
		try {
			dbObj = handle.getConnection(SQLiteDatabase.OPEN_READWRITE);
			ContentValues contents = new ContentValues();
//			contents.put(FIELD_TABLET_ID, dbData.getId());
			contents.put(FIELD_TABLET_NAME, AddPageActivity.dbData.getTabletname());
			contents.put(FIELD_DOSAGE, AddPageActivity.dbData.getDoasge());
			contents.put(FIELD_SCHEDULE, AddPageActivity.dbData.getSchedule());
			contents.put(FIELD_ALARM_TIME, AddPageActivity.dbData.getAlarmtime());
			contents.put(FIELD_TONES, AddPageActivity.dbData.getTones());
			Log.i(TagProject, TagPage+"inserting...");
			dbObj.insert(TABLE_NAME, null, contents);
			Log.i(TagProject, TagPage+"inserted...");
		} finally {
			if (dbObj != null) {
				dbObj.close();
			}
		}
	}
	
	public void delete(TabletDB handle) throws SQLException {
		Log.i(TagProject, TagPage+"delete called...");
		SQLiteDatabase dbObj = null;
		try {
			dbObj = handle.getConnection(SQLiteDatabase.OPEN_READWRITE);
			final String deleteQUERY =FIELD_TABLET_ID+" = "+AddPageActivity.dbData.getId();
			dbObj.delete(TABLE_NAME, deleteQUERY , null);
			Log.i(TagProject, TagPage+"deleted...");
			//Logger.d(tag, "delete() affected rows: " + rowsAffected);
		} finally {
			if (dbObj != null) {
				dbObj.close();
			}
		}
	}
	
	public void update(TabletDB handle) throws SQLException {
		Log.i(TagProject, TagPage+"update called...");
		SQLiteDatabase dbObj = null;
		try {
			dbObj = handle.getConnection(SQLiteDatabase.OPEN_READWRITE);
			ContentValues contents = new ContentValues();
			contents.put(FIELD_TABLET_NAME, AddPageActivity.dbData.getTabletname());
			contents.put(FIELD_DOSAGE, AddPageActivity.dbData.getDoasge());
			contents.put(FIELD_SCHEDULE, AddPageActivity.dbData.getSchedule());
			contents.put(FIELD_ALARM_TIME, AddPageActivity.dbData.getAlarmtime());
			contents.put(FIELD_TONES, AddPageActivity.dbData.getTones());
			final String updateQUERY =	FIELD_TABLET_ID+" = "+AddPageActivity.dbData.getId();
			Log.i(TagProject, TagPage+"updating..."+updateQUERY);
			dbObj.update(TABLE_NAME, contents, updateQUERY, null);
			Log.i(TagProject, TagPage+"updated...");
		} finally {
			if (dbObj != null) {
				dbObj.close();
			}
		}
	}
}
